"use strict";

var m_Ability = -1;
var m_QueryUnit = -1;

var m_isShop = false;
var m_isControlled = false;

var m_auto_p = null;
var m_lvup_p = null;
var m_techData = null;

var m_timer = null;
function SetAbility( ability, queryUnit, techData )
{
	$('#HotkeyContainer').visible = false;
	var bChanged = ( ability !== m_Ability || queryUnit !== m_QueryUnit );
	if(!bChanged){
		return;
	}
	m_Ability = ability;
	m_QueryUnit = queryUnit;
	m_techData = techData;
	var canUpgradeRet = Abilities.CanAbilityBeUpgraded( m_Ability );
	var canUpgrade = ( canUpgradeRet == AbilityLearnResult_t.ABILITY_CAN_BE_UPGRADED );

	$.GetContextPanel().SetHasClass( "no_ability", ( ability == -1 ) );
	//$.GetContextPanel().SetHasClass( "learnable_ability", bInLevelUp && canUpgrade );
	if(ability!=-1){
		GameEvents.Subscribe( "C2S_GET_ITEM_DATA"+ability, C2S_GET_ITEM_DATA );
	}
	UpdateAbility();
}

function AutoUpdateAbility()
{
	UpdateAbility();
	$.Schedule( 0.1, AutoUpdateAbility );
}

function onClickAbility(){
	Abilities.ExecuteAbility( m_Ability, m_QueryUnit, true );
}

function UpdateAbility()
{
	if (m_Ability==-1){

		return;
	}
	var abilityButton = $( "#AbilityButton" );
	var abilityName = Abilities.GetAbilityName( m_Ability );

	var noLevel =( 0 == Abilities.GetLevel( m_Ability ) );
	var isCastable = !Abilities.IsPassive( m_Ability ) && !noLevel;
	var manaCost = Abilities.GetManaCost( m_Ability );
	var hotkey = Abilities.GetKeybind( m_Ability, m_QueryUnit );
	var unitMana = Entities.GetMana( m_QueryUnit );

	$.GetContextPanel().SetHasClass( "no_level", noLevel );
	$.GetContextPanel().SetHasClass( "is_passive", Abilities.IsPassive(m_Ability) );
	$.GetContextPanel().SetHasClass( "no_mana_cost", ( 0 == manaCost ) );
	$.GetContextPanel().SetHasClass( "insufficient_mana", ( manaCost > unitMana ) );
	$.GetContextPanel().SetHasClass( "auto_cast_enabled", Abilities.GetAutoCastState(m_Ability) );
	$.GetContextPanel().SetHasClass( "toggle_enabled", Abilities.GetToggleState(m_Ability) );
	$.GetContextPanel().SetHasClass( "is_active", ( m_Ability == Abilities.GetLocalPlayerActiveAbility() ) );

	abilityButton.enabled = ( isCastable );
	
	$( "#HotkeyText" ).text = hotkey;
	
	$( "#AbilityImage" ).abilityname = abilityName;
	$( "#AbilityImage" ).contextEntityIndex = m_Ability;
	
	$( "#ManaCost" ).text = manaCost;
	
	if ( Abilities.IsCooldownReady( m_Ability ) )
	{
		$.GetContextPanel().SetHasClass( "cooldown_ready", true );
		$.GetContextPanel().SetHasClass( "in_cooldown", false );
		$( "#CooldownOverlay" ).SetHasClass("toaaa",false);
	}
	else
	{
		$.GetContextPanel().SetHasClass( "cooldown_ready", false );
		$.GetContextPanel().SetHasClass( "in_cooldown", true );
		var cooldownLength = Abilities.GetCooldownLength( m_Ability );
		var cooldownRemaining = Abilities.GetCooldownTimeRemaining( m_Ability );
		var cooldownPercent = Math.ceil( 100 * cooldownRemaining / cooldownLength );
		$( "#CooldownTimer" ).text = Math.ceil( cooldownRemaining );
		var q = (cooldownRemaining / cooldownLength * 360);
		$( "#CooldownOverlay" ).style.clip = "radial( 50.0% 50.0%, 0.0deg,-"+q+"deg)";
	}

	// if (Abilities.CanAbilityBeUpgraded( m_Ability ) == 0 && Entities.GetAbilityPoints( m_Ability ) > 0 && Entities.IsHero( m_QueryUnit ) && canLevelup()){
	// 	$('#LevelUpTab').visible = true;
	// }else{
	// 	$('#LevelUpTab').visible = false;
	// }
	
}

function canLevelup(){
	var maxlv = Abilities.GetMaxLevel( m_Ability );
	var curlv = Abilities.GetLevel( m_Ability );
	
	return curlv < maxlv;
}
var tooltipflag = false;
//这个标志位的作用在于处理当数据还没获取到的时候，我已经触发了mouseOut 从而导致先out再over的尴尬情况
function MouseOver(){
	tooltipflag = true
	GameEvents.SendCustomGameEventToServer( "C2S_GET_ITEM_DATA", {ability:m_Ability,unit:m_QueryUnit} );
}
function C2S_GET_ITEM_DATA(data){
	if(tooltipflag){
		$.DispatchEvent("UIShowCustomLayoutParametersTooltip", $.GetContextPanel(),"CustomItemNode",
		"file://{resources}/layout/custom_game/CustomItemNode.xml",
		"buildState="+JSON.stringify(data.state)+"&buildTree="+JSON.stringify(m_techData)+"&abilityIndex="+m_Ability);

	}
}
 
function MouseOut(){
	tooltipflag = false;
	$.DispatchEvent( "UIHideCustomLayoutTooltip", $.GetContextPanel(),"CustomItemNode");
}

function ActivateAbility()
{

	if (!Abilities.IsCooldownReady(m_Ability)){
		GameUI.onErrorMsg({text:"NOTICE_IN_CD"})
		return;
	}
	if(0 == Abilities.GetLevel( m_Ability )){
		GameUI.onErrorMsg({text:"NOTICE_NO_LEVEL"})
		return;
	}
	
	Abilities.ExecuteAbility( m_Ability, m_QueryUnit, false );
	
	//
}

function LevelUpAbility(){
	Abilities.AttemptToUpgrade( m_Ability );
}

function DoubleClickAbility()
{
	// Handle double-click like a normal click - ExecuteAbility will either double-tap (self cast) or normal toggle as appropriate
	ActivateAbility();
}

function RightClickAbility()
{

	if ( Abilities.IsAutocast( m_Ability ) )
	{
		Game.PrepareUnitOrders( { OrderType: dotaunitorder_t.DOTA_UNIT_ORDER_CAST_TOGGLE_AUTO, AbilityIndex: m_Ability } );
	}
}

function hiddenHotkey(bol){

	//$('#HotkeyContainer').visible = bol;
}

(function()
{
	$.GetContextPanel().SetAbility = SetAbility;
	$.GetContextPanel().hiddenHotkey = hiddenHotkey;
	AutoUpdateAbility(); // initial update of dynamic state
})();
